package com.example.hudongwx.myclass0719application.fragment;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.hudongwx.myclass0719application.R;

/**
 *
 * 自定义的控件
 * Created by hudongwx on 16-10-28.
 */
public class Inputview extends FrameLayout {
    private EditText editText;
    private ImageView imageView;
    private String myHint, myText;
    private int myColor, mySize;
    private int myicon;

    public Inputview(Context context) {
        super(context);
        initView(context);
    }

    public Inputview(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.my_input_view);
        myColor=array.getColor(R.styleable.my_input_view_mycolor, Color.BLACK);
        mySize=array.getDimensionPixelSize(R.styleable.my_input_view_mysize,30);
        myicon=array.getResourceId(R.styleable.my_input_view_myicon,0);
        myHint=array.getString(R.styleable.my_input_view_myhint);
        myText=array.getString(R.styleable.my_input_view_mytext);
      //  array.recycle();

        editText.setText(myText);
        editText.setTextColor(myColor);
        editText.setHint(myHint);
        editText.setTextSize(mySize);
        if (myicon!=0){
            Drawable drawable=getResources().getDrawable(myicon);
            //设置资源大小和上下左右的图片
            drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
            editText.setCompoundDrawables(drawable,null,null,null);
        }
    }


    //初始化布局
    private void initView(Context context){
        View view= LayoutInflater.from(context).inflate(R.layout.inputview,this,true);
        editText= (EditText) view.findViewById(R.id.input_edittext);
        imageView= (ImageView) view.findViewById(R.id.input_imageview);
        //为edittext添加改变的监听器
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
     if (TextUtils.isEmpty(s)){
           imageView.setVisibility(INVISIBLE);
     }else {
            imageView.setVisibility(VISIBLE);
     }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //实现点击图片清空edittext
imageView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
           editText.setText("");
    }
});

    }

//get和set
    public String getMyText() {
        return myText;
    }

    public void setMyText(String myText) {
        this.myText = myText;
    }

    public int getMyColor() {
        return myColor;
    }

    public void setMyColor(int myColor) {
        this.myColor = myColor;
    }

    public int getMySize() {
        return mySize;
    }

    public void setMySize(int mySize) {
        this.mySize = mySize;
        Drawable drawable=getResources().getDrawable(myicon);
        //设置资源大小和上下左右的图片
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        editText.setCompoundDrawables(drawable,null,null,null);
    }

    public int getMyicon() {
        return myicon;

    }

    public void setMyicon(int myicon) {
        this.myicon = myicon;
    }




    public String getMyHint() {
        return myHint;
    }

    public void setMyHint(String myHint) {
        this.myHint = myHint;
        editText.setHint(myHint);
    }

    //定义控件显示类型
    public void  setinputType(int inputType){
        editText.setInputType(inputType);

    }
    public int getinputType(){
        return editText.getInputType();
    }
}
